package com.macro.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.Roll;
import com.macro.mall.portal.domain.User;
import com.macro.mall.portal.domain.UserRoll;
import com.macro.mall.portal.domain.vo.WxMsgVo;
import com.macro.mall.portal.dto.UserRollDto;
import com.macro.mall.portal.service.IRollService;
import com.macro.mall.portal.service.IUserRollService;
import com.macro.mall.portal.service.IUserService;
import com.macro.mall.portal.util.ConfigConstant;
import com.macro.mall.portal.util.InfoFilterUtil;
import com.macro.mall.portal.util.WxMsgPush;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 用户领取消费卷记录 前端控制器
 *
 * @author
 * @since 2021-09-11
 */
@RestController
@RequestMapping("/userRoll")
@Api(tags = "消费卷", description = "消费卷")
public class UserRollController {

    @Autowired
    private IUserRollService iUserRollService;

    @Autowired
    private IRollService iRollService;

    @Autowired
    private WxMsgPush wxMsgPush;

    @Autowired
    private IUserService vxUserService;

    @ApiOperation("添加消费卷记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody UserRoll userRoll) {
        try {
            iUserRollService.save(userRoll);
            String url = ConfigConstant.XFJ_URL + "?id=" + userRoll.getId();

            LocalDateTime time = LocalDateTime.now();

            DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            String now = dtf2.format(time);
            //todo 文案填写
            WxMsgVo msgVo = WxMsgVo.builder()
                    .first("您好！客户已领取贷款佣金券")
                    .keyword1(userRoll.getName() + "｜" + userRoll.getPhone())
                    .keyword2(now)
                    .openId(userRoll.getOpenId())
                    .url(url)
                    .remark("点击查看详情-查看客户信息")
                    .build();
            wxMsgPush.SendWxMsg(msgVo);
            //上级领取优惠价
            QueryWrapper<User> user = new QueryWrapper<>();
            user.eq("open_id", userRoll.getOpenId());
            User one = vxUserService.getOne(user);
            if (one.getSuperId() != 0) {
                User sub = vxUserService.getById(one.getSuperId());
                String url2 = ConfigConstant.XFJ_URL2 + "?id=" + userRoll.getId();
                WxMsgVo msg = WxMsgVo.builder()
                        .first("您好！下级合伙人推荐客户已领取贷款佣金券")
                        .keyword1(InfoFilterUtil.maskUserName(userRoll.getName()) + "｜" + InfoFilterUtil.mobileEncrypt(userRoll.getPhone()))
                        .keyword2(now)
                        .openId(sub.getOpenId())
                        .url(url2)
                        .remark("点击查看详情-领取信息")
                        .build();
                wxMsgPush.SendWxMsg(msg);
            }
            return CommonResult.success(null, "领取成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("查询所有消费卷")
    @RequestMapping(value = "/getRoll", method = RequestMethod.POST)
    public CommonResult getRoll() {
        try {
            List<Roll> rollList = iRollService.list();
            return CommonResult.success(rollList);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("合伙人分页消费卷记录")
    @RequestMapping(value = "/salesmanPageList", method = RequestMethod.POST)
    public CommonResult salesmanPageList(@RequestBody UserRollDto dto) {
        try {
            PageHelper.startPage(dto.getPage(), dto.getSize());
            QueryWrapper<UserRoll> wrapper = new QueryWrapper<>();
            wrapper.eq("open_id", dto.getOpenId());
            wrapper.eq("speed_status", dto.getLoanStatus());
            List<UserRoll> rollList = iUserRollService.list(wrapper);
            PageInfo<UserRoll> page = new PageInfo<UserRoll>(rollList, dto.getSize());
            return CommonResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

//    @ApiOperation("用户分页消费卷记录")
//    @RequestMapping(value = "/userPageList", method = RequestMethod.POST)
//    public CommonResult userPageList(@RequestBody UserRollDto dto) {
//        try {
//            PageHelper.startPage(dto.getPage(), dto.getSize());
//            QueryWrapper<UserRoll> wrapper = new QueryWrapper<>();
//            wrapper.eq("open_id", dto.getOpenId());
//            wrapper.eq("speed_status", dto.getLoanStatus());
//            List<UserRoll> rollList = iUserRollService.list(wrapper);
//            PageInfo<UserRoll> page = new PageInfo<UserRoll>(rollList, dto.getSize());
//            return CommonResult.success(page);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return CommonResult.failed("服务器异常");
//        }
//    }

    @ApiOperation("消费卷查询信息")
    @RequestMapping(value = "/getInfo", method = RequestMethod.POST)
    public CommonResult getInfo(@RequestBody UserRoll userRoll) {
        try {
            QueryWrapper<UserRoll> wrapper = new QueryWrapper<>();
            wrapper.eq("id", userRoll.getId());
            UserRoll one = iUserRollService.getOne(wrapper);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("分页查询消费卷")
    @RequestMapping(value = "/getPageRoll", method = RequestMethod.POST)
    public CommonResult getPageRoll(@RequestBody UserRollDto dto) {
        try {
            PageHelper.startPage(dto.getPage(), dto.getSize());
            QueryWrapper<UserRoll> wrapper = new QueryWrapper<>();
            wrapper.eq("phone", dto.getPhone());
            List<UserRoll> rollList = iUserRollService.list(wrapper);
            PageInfo<UserRoll> page = new PageInfo<UserRoll>(rollList, dto.getSize());
            return CommonResult.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("使用消费卷")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody UserRoll userRoll) {
        try {
            userRoll.setLoanStatus(2);
            iUserRollService.updateById(userRoll);
            return CommonResult.success(null, "使用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }


}
